﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cofe.Core.Logger
{   
    public class EventListener<T> : IEventListener<T>
        where T : EventArgs
    {
        #region Constructor
        protected EventListener(CancellationToken token)
        {
            CancellationToken = token;
        }

        protected EventListener()
        {
            _cts = new CancellationTokenSource();
            CancellationToken = _cts.Token;
        }

        public EventListener(Action<IEnumerable<IEventLog<T>>> handleAction)
            : this()
        {
            _handleAction = handleAction;
        }

        public EventListener(Action<IEnumerable<IEventLog<T>>> handleAction, CancellationToken token)
            : this(token)
        {
            _handleAction = handleAction;
        }
        #endregion

        #region Methods

        public virtual void DispatchEvent(IEnumerable<IEventLog<T>> eventsRecorded)
        {
            if (_handleAction != null)
                _handleAction(eventsRecorded);
        }

        public virtual void DispatchEvent(IEnumerable<IEventLog<EventArgs>> eventsRecorded)
        {

        }


        public void Dispose()
        {
            if (_cts != null)
                _cts.Cancel();
        }
        
        #endregion

        #region Data
                
        Action<IEnumerable<IEventLog<T>>> _handleAction = null;
        private CancellationTokenSource _cts = null;

        #endregion

        #region Public Properties

        public CancellationToken CancellationToken { get; private set; }

        #endregion
        

    }
}
